package light.head.dao.inv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.inv.InvReceipts;
@Service
public class InvReceiptsDAO extends BaseDAO<InvReceipts, Integer>{
	
	
	public RESULT findByMap(List<Integer> ocs, Map<String,String> map, PageBean pb) {
		if (ocs == null ) return null;
		
		StringBuffer hql = new StringBuffer("FROM InvReceipts ir WHERE ir.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		
		if (null != map) {
			String type = map.get(Parm.TYPE);
			String dn = map.get(Parm.DN);
			String cn = map.get(Parm.CN);
			String on = map.get(Parm.ON);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			String bn = map.get(Parm.BN);
			if (isNotNull(type)) {
				list.add(Byte.parseByte(type));
				hql.append(" AND ir.type LIKE ?");
			}
			if (isNotNull(dn)) {
				list.add("%" + dn + "%");
				hql.append(" AND ir.dn LIKE ?");
			}if (isNotNull(cn)) {
				list.add("%" + cn + "%");
				hql.append(" AND ir.cu IN (SELECT ui.code FROM UserInfo ui WHERE ui.realName LIKE ?)");
			}if (isNotNull(on)) {
				list.add("%" + on + "%");
				hql.append(" AND ir.oc IN (SELECT o.code FROM Organ o WHERE o.name LIKE ?)");
			}
			if (isNotNull(bn)) {
				list.add("%" + bn + "%");
				hql.append(" AND ir.bc IN (SELECT b.code FROM InvBusiness b WHERE b.name LIKE ?)");
			}if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND ir.ct >= ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append(" AND ir.ct <= ?");
			}
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND ir.deleted = ?");
			}
		}
		hql.append(" ORDER BY ir.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	public List<InvReceipts> findByDnOc(String dn, Integer oc){
		if (dn == null || oc == null) return null;
		StringBuffer hql = new StringBuffer("FROM InvReceipts ir WHERE ir.dn = ? AND ir.oc = ? ");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(dn);
		parm.add(oc);
		
		return find(hql.toString(), parm.toArray());
	}
}
